package top.rish.universe.core.plugin;

import freemarker.core.Environment;
import freemarker.template.*;
import lombok.Setter;
import org.noear.solon.Solon;
import org.noear.solon.core.util.LogUtil;
import org.noear.solon.view.freemarker.FreemarkerRender;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.UUID;

@Setter
public class MyExceptionHandler implements TemplateExceptionHandler {

    Template template;


    @Override
    public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
        try {
            LogUtil.global().error(te.toString());
            if(null == this.template){
                return;
            }
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("error",te.toString());
            hashMap.put("errorId", UUID.randomUUID().toString());
            template.process(hashMap, out);
            out.flush();
        } catch (IOException e) {
            throw new TemplateException("Error writing error message. Cause: " + e, env);
        }
    }
}